package insert;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import utility.connections;

public class Insert_richiesta extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String oracleUser = "azienda";
    private String oraclePwd  = "azienda";
    private Connection conn;
    private String driver,oracle;
    private String cmbSCat,motivazione,dipendente;
	
	
    public Insert_richiesta() {
        super();
    }
    
    public void init(ServletConfig sc) throws ServletException
    {
    	super.init(sc);
		driver = "oracle.jdbc.driver.OracleDriver";
		oracle = "jdbc:oracle:thin:@localhost:1521:xe";
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String qry="";
		cmbSCat=request.getParameter("cmbSCat");
		motivazione=request.getParameter("motivazione");
		dipendente=request.getParameter("dipendente");
		try {
			conn = DriverManager.getConnection(oracle, oracleUser, oraclePwd);
			conn.setAutoCommit(false);
			Statement st = conn.createStatement();
			qry = "insert into richiesta (DIPENDENTE,SOTTOCATEGORIABENE,DATA,MOTIVAZIONE,ESITO) "
				   + "values('"+dipendente+"','"+cmbSCat+"',SYSDATE,'"+motivazione+"','P')";
			System.out.println(qry);
			ResultSet rs = st.executeQuery(qry);
			rs.close();
			conn.commit();
			st.close();
			conn.close();
			
			response.sendRedirect("dip/richieste/visualizza.jsp?order=7&dipendente="+dipendente);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

}
